<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/12
 * Time: 17:29
 */

namespace app\service;


use app\index\controller\Base;

class LDService extends BaseServcice
{


    public function nID_($func)
    {
        $this->contract->call('nID_', function ($err, $result) use ($func) {
            if ($result) {
                $nums = intval($result[0]->toString());
                $this->redis->_set("nID_", $nums, 120);
                $func(true);
            } else {
                $func(false, $err->getMessage());
            }
        });
    }


    public function lastPotTime($func)
    {
        $this->contract->call('lastPotTime', function ($err, $result) use ($func) {
            if ($result) {
                $res = intval($result[0]->toString());
                $this->redis->_set("lastPotTime", $res, 120);
                $func(true);
            } else {
                $func(false, $err->getMessage());
            }
        });
    }

    public function luckNum($nID_, $func)
    {
        $body = [];
        // 获取numLucks 中奖号码
        $this->contract->call("numLucks_", $nID_, function ($err, $res) use ($body, $nID_, $func) {
            if ($res) {
                $body["num"] = intval($res[0]->toString());
                // 获取每期开奖金额
                $this->contract->call("numPot_", $nID_, function ($err, $res) use ($body, $nID_, $func) {
                    if ($res) {
                        $body['pot'] = intval($res[0]->toString());
                    } else {
                        $func(false, $err->getMessage());
                    }
                    // 获取中奖用户长度
                    $this->contract->call("winnerLength", $nID_, function ($err, $res) use ($body, $nID_, $func) {
                        if ($res) {
                            $length = intval($res[0]->toString());
                            $body['winner'] = [];
                            // 判断是否永久缓存（当前查询轮次小于最大的轮次，则永久缓存数据）
                            $NID = 1;
                            if ($this->redis->_exists("nID_")) {
                                $NID = $this->redis->_get("nID_");
                            }
                            if ($length > 0) {
                                for ($i = 0; $i < $length; $i++) {
                                    // 获取幸运用户
                                    $this->contract->call("winners", $i, function ($err, $res) use (&$body, $nID_, $NID, $func, $i, $length) {
                                        if ($res) {
                                            $body['winner'][$i] = $res[0]->toString();
                                            if ($i == ($length - 1)) {
                                                if ($nID_ < $NID) {
                                                    $this->redis->_set("luckNum_" . $nID_, $body, 12000000);
                                                } else {
                                                    $this->redis->_set("luckNum_" . $nID_, $body, 120);
                                                }
                                                $func(true);
                                            }
                                        } else {
                                            $func(false, $err->getMessage());
                                        }
                                    });
                                }
                            } else {
                                if ($nID_ < $NID) {
                                    $this->redis->_set("luckNum_" . $nID_, $body, 12000000);
                                } else {
                                    $this->redis->_set("luckNum_" . $nID_, $body, 120);
                                }
                                $func(true);
                            }

                        } else {
                            $func(false, $err->getMessage());
                        }
                    });
                });
            } else {
                $func(false, $err->getMessage());
            }
        });
    }

    public function pot($func)
    {
        $this->contract->call('round_', 1, function ($err, $result) use ($func) {
            if ($result) {
                $data = [
                    "pot" => $result['pot']->toString(),
                    "eth" => $result['eth']->toString(),
                    "keys" => $result['keys']->toString(),
                ];
                $this->redis->_set("pot", $data, 120);
                $func(true);
            } else {
                $func(false, $err->getMessage());
            }
        });
    }

    public function pIDxAddr($address, $func)
    {
        $this->contract->call('pIDxAddr_', $address, function ($err, $result) use ($address, $func) {
            if ($result) {
                $res = intval($result[0]->toString());
                $this->redis->_set($address, $res, 12000000000000);
                $func(true);
            } else {
                $func(false, $err->getMessage());
            }
        });
    }

    public function plyr($pID, $func)
    {
        $this->contract->call('plyr_', $pID, function ($err, $result) use ($pID, $func) {
            if ($result) {
                $res = [
                    "addr" => $result["addr"],
                    "name" => $result["name"],
                    "withdraw" => $result["withdraw"]->toString()
                ];
                $this->redis->_set("plyr_" . $pID, $res, 120);
                $func(true);
            } else {
                $func(false, $err->getMessage());
            }
        });
    }

}